package com.example.lowbird.lowbird.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.lowbird.lowbird.R;
import com.example.lowbird.lowbird.utils.Dbutils;

/**
 * Created by Administrator on 2016/11/2.
 */

public class Shoptext extends LinearLayout {
    private View view;
    private Button shopadd;
    private Button shopdele;
    private TextView shoptext;
    private int maxsize = 100;
    private int minsize = 1;
    private int textcount = 1;
    private int percoun = 1;
    private TypedArray typedArray;

    public int getPercoun() {
        return percoun;
    }

    public void setPercoun(int percoun) {
        this.percoun = percoun;
    }

    public int getMaxsize() {
        return maxsize;
    }

    public void setMaxsize(int maxsize) {
        this.maxsize = maxsize;
    }

    public int getMinsize() {
        return minsize;
    }

    public void setMinsize(int minsize) {
        this.minsize = minsize;
    }

    public int getTextcount() {

        return textcount;
    }

    public void setTextcount(int textcount) {

        if (textcount < minsize || textcount > maxsize) {
            Log.e("SHOP", "setTextcount: 对不起，请输入合理数据");
            return;
        }
        Log.e("SHOP", "setTextcount2: 对不起，请输入合理数据");
        shoptext.setText(textcount + "");
        this.textcount = textcount;
    }

    public Shoptext(Context context) {
        this(context, null);
    }

    public Shoptext(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Shoptext(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        Log.e("SHOP", "Shoptext: " + context.toString());
        typedArray = context.obtainStyledAttributes(attrs, R.styleable.shopcar);
        maxsize = typedArray.getInt(R.styleable.shopcar_maxsize, 100);
        minsize = typedArray.getInt(R.styleable.shopcar_minsize, 1);
        textcount = typedArray.getInt(R.styleable.shopcar_textcount, 1);
        view = View.inflate(context, R.layout.shopcar, this);
        initview(view);
        setTextcount(textcount);

    }

    public void initview(View view) {
        shopadd = ((Button) view.findViewById(R.id.shopadd));
        shoptext = ((TextView) view.findViewById(R.id.shoptext));
        shopdele = ((Button) view.findViewById(R.id.shopdelet));

        shoptext.setText("jj");
        shopadd.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {


                int lastcount = textcount + percoun;
                if (lastcount < maxsize) {
                    if (callback != null) {

                        callback.addcunt(lastcount);
                    }
                    setTextcount(lastcount);
                } else {

                    Log.e("SHOPCAR", "onClick:对不起库存不足 ");
                }


            }
        });

        shopdele.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                int lastcount = textcount - percoun;
                if (lastcount > 0) {
                    setTextcount(lastcount);
                    if (callback != null) {

                        callback.delecount(lastcount);
                    }
                } else if (lastcount == 0) {
                    callback.deletitem();
                    Log.e("SHOPCAR", "已经是最小数量了 ");
                }

            }
        });

    }


    public interface ShopcarInterface {


        public void addcunt(int lastcount);

        public void delecount(int lastcount);

        public void deletitem();

    }

    public ShopcarInterface callback;


    public ShopcarInterface getCallback() {
        return callback;
    }

    public void setCallback(ShopcarInterface callback) {
        this.callback = callback;
    }
}
